<?php
/**
 * CalendarController.php
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Blog
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Blog_CalendarController
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Blog
 */
class Blog_CalendarController extends Madoqua_Controller_Action_Abstract 
{

    /**
     * Post model
     *
     * @var Blog_Post
     */
    private $_postModel;

    /**
     * initialization
     */
    public function init()
    {
        parent::init();

        $this->_modelLoader->load(array('Blog_Post'));
        $this->_postModel = new Blog_Post();
        //set up models
    }    
    
    /**
     * calendar index
     * 
     * @todo add param for start/$now
     */
    public function monthAction()
    {
        $count = (int) $this->getRequest()->getParam('count', 12);
        //number of months to count back

        $now = time();
        //it's... now!
        
        $posts = array();
        //post array

        $begin = mktime(0, 0, 0, strftime('%m', $now) - $count, 1, strftime('%y', $now));
        //start of $count months back from $now        
        $posts = $this->_postModel->fetchByTimeSpan($begin, $now);
        //all the posts from $begin to $now
        
        $months = array();
        foreach($posts as $post) {
            $year = (int) strftime('%y', $post->posted);
            $month = (int) strftime('%m', $post->posted);
            $months[$year][$month][] = $post;
            //put posts into months
        }
        
        $this->view->months = $months;
        //the posts
        $this->view->start = $begin;
        $this->view->end = mktime(0, 0, 0, strftime('%m', $now) - 12, 1, strftime('%y', $now));
        //start and end
        
    }
}